package com.ruoyi.info.userController;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.info.domain.FhInfoTasklist;
import com.ruoyi.info.domain.FhInfoUser;
import com.ruoyi.info.domain.FhInfoUsertask;
import com.ruoyi.info.service.IFhInfoTasklistService;
import com.ruoyi.info.service.IFhInfoUserService;
import com.ruoyi.info.service.IFhInfoUsertaskService;
import com.ruoyi.tokenTools.UserLoginToken;
import com.test.aop.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 用户任务完成度表Controller
 * 
 * @author ruoyi
 * @date 2024-11-09
 */
@Api(tags = "用户任务完成度表")
@RestController
@RequestMapping("/info/userUsertask")
public class UserFhInfoUsertaskController extends BaseController
{
    @Autowired
    private IFhInfoUsertaskService iFhInfoUsertaskService;

    @Autowired
    private IFhInfoTasklistService iFhInfoTasklistService;

    @Autowired
    private IFhInfoUserService fhInfoUserService;

    /**
     * 用户完成任务加积分
     */
    public R<String> compleTask(Long taskId)
    {
        try {
            //修改信息表
            Long userId = UserThreadLocal.getUser();
            FhInfoUser fhInfoUserInfo = fhInfoUserService.selectFhInfoUserByUserId(userId);
            Long points = fhInfoUserInfo.getPoints();
            points+=10;
            fhInfoUserInfo.setPoints(points);
            fhInfoUserService.updateFhInfoUser(fhInfoUserInfo);
            //修改任务表状态，1为未完成，0为完成
            FhInfoUsertask fhInfoUsertask = new FhInfoUsertask();
            fhInfoUsertask.setUserId(userId);
            fhInfoUsertask.setTaskId(taskId);
            fhInfoUsertask.setCompletionStatus("0");
            iFhInfoUsertaskService.updateFhInfoUsertaskByUserIdAndTaskId(fhInfoUsertask);
            return R.ok("完成成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询用户任务完成度表列表
     */
    @Anonymous
    @UserLoginToken
    @ApiOperation("查询用户任务完成度表列表")
    @GetMapping("/list")
    public R<List<FhInfoUsertask>> list()
    {
       try{
           FhInfoUsertask fhInfoUsertask = new FhInfoUsertask();
           fhInfoUsertask.setUserId(UserThreadLocal.getUser());
           List<FhInfoTasklist> fhInfoTasklists = iFhInfoTasklistService.selectFhInfoTasklistList(new FhInfoTasklist());
           List<FhInfoUsertask> list = iFhInfoUsertaskService.selectFhInfoUsertaskList(fhInfoUsertask);
           //用户任务列表为空
           if(list.isEmpty()){
               for (int i = 0; i < fhInfoTasklists.size(); i++) {
                   BeanUtils.copyProperties(fhInfoTasklists.get(i),fhInfoUsertask);
                   fhInfoUsertask.setTaskTime(new Date());
                   fhInfoUsertask.setTaskId(fhInfoTasklists.get(i).getId());
                   iFhInfoUsertaskService.insertFhInfoUsertask(fhInfoUsertask);
               }
           }else {
               //用户任务列表不为空,先判断任务日期是否为同一天，不是的话则
               // 先删除每日任务，再刷新每日任务
               if(!DateUtils.isSameDay(list.get(0).getTaskTime(),new Date())){
                   for (int i = 0; i < list.size(); i++) {
                       if(list.get(i).getTaskType().equals("0")){
                           iFhInfoUsertaskService.deleteFhInfoUsertaskById(list.get(i).getId());
                       }
                   }
                   for (int i = 0; i < fhInfoTasklists.size(); i++) {
                       //0是每日任务，1是一次性任务
                       if (fhInfoTasklists.get(i).getTaskType().equals("0")){
                           FhInfoUsertask newFhInfoUsertask = new FhInfoUsertask();
                           BeanUtils.copyProperties(list.get(i),newFhInfoUsertask);
                           newFhInfoUsertask.setTaskId(fhInfoTasklists.get(i).getId());
                           iFhInfoUsertaskService.insertFhInfoUsertask(newFhInfoUsertask);
                       }else {
                           // 任务类型为一次性任务，如果用户任务表表中有该一次性任务，则不添加
                           Boolean flag = true;
                           for (int j = 0; j < list.size(); j++) {
                               if(fhInfoTasklists.get(i).getTask().equals(list.get(j).getTask()) ){
                                   flag = false;
                               }
                           }
                           if(flag){
                               FhInfoUsertask newFhInfoUsertask = new FhInfoUsertask();
                               BeanUtils.copyProperties(list.get(i),newFhInfoUsertask);
                               newFhInfoUsertask.setTaskId(fhInfoTasklists.get(i).getId());
                               iFhInfoUsertaskService.insertFhInfoUsertask(newFhInfoUsertask);
                           }
                       }
                   }
               }
           }
           FhInfoUsertask fhInfoUsertask1 = new FhInfoUsertask();
           fhInfoUsertask1.setUserId(fhInfoUsertask.getUserId());
           List<FhInfoUsertask> fhInfoUsertasks = iFhInfoUsertaskService.selectFhInfoUsertaskList(fhInfoUsertask1);
           return R.ok(fhInfoUsertasks);
       }catch (Exception e){
           return R.fail(e.getMessage());
       }
    }

    //每日更新用户任务表
    @Scheduled(cron = "0 0 0 1/1 * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void updateUserTask(){
        FhInfoUser fhInfoUser = new FhInfoUser();
        List<FhInfoUser> fhInfoUserList = fhInfoUserService.selectFhInfoUserList(fhInfoUser);
        for (int i1 = 0; i1 < fhInfoUserList.size(); i1++) {
            Long userId = fhInfoUserList.get(i1).getUserId();

            FhInfoUsertask fhInfoUsertask = new FhInfoUsertask();
            fhInfoUsertask.setUserId(userId);
            List<FhInfoTasklist> fhInfoTasklists = iFhInfoTasklistService.selectFhInfoTasklistList(new FhInfoTasklist());
            List<FhInfoUsertask> list = iFhInfoUsertaskService.selectFhInfoUsertaskList(fhInfoUsertask);
            //用户任务列表为空
            if(list.isEmpty()){
                for (int i = 0; i < fhInfoTasklists.size(); i++) {
                    BeanUtils.copyProperties(fhInfoTasklists.get(i),fhInfoUsertask);
                    fhInfoUsertask.setTaskTime(new Date());
                    fhInfoUsertask.setTaskId(fhInfoTasklists.get(i).getId());
                    iFhInfoUsertaskService.insertFhInfoUsertask(fhInfoUsertask);
                }
            }else {
                //用户任务列表不为空,先判断任务日期是否为同一天，不是的话则
                // 先删除每日任务，再刷新每日任务
                if(!DateUtils.isSameDay(list.get(0).getTaskTime(),new Date())){
                    for (int i = 0; i < list.size(); i++) {
                        if(list.get(i).getTaskType().equals("0")){
                            iFhInfoUsertaskService.deleteFhInfoUsertaskById(list.get(i).getId());
                        }
                    }
                    for (int i = 0; i < fhInfoTasklists.size(); i++) {
                        //0是每日任务，1是一次性任务
                        if (fhInfoTasklists.get(i).getTaskType().equals("0")){
                            FhInfoUsertask newFhInfoUsertask = new FhInfoUsertask();
                            BeanUtils.copyProperties(list.get(i),newFhInfoUsertask);
                            newFhInfoUsertask.setTaskId(fhInfoTasklists.get(i).getId());
                            iFhInfoUsertaskService.insertFhInfoUsertask(newFhInfoUsertask);
                        }else {
                            // 任务类型为一次性任务，如果用户任务表表中有该一次性任务，则不添加
                            Boolean flag = true;
                            for (int j = 0; j < list.size(); j++) {
                                if(fhInfoTasklists.get(i).getTask().equals(list.get(j).getTask()) ){
                                    flag = false;
                                }
                            }
                            if(flag){
                                FhInfoUsertask newFhInfoUsertask = new FhInfoUsertask();
                                BeanUtils.copyProperties(list.get(i),newFhInfoUsertask);
                                newFhInfoUsertask.setTaskId(fhInfoTasklists.get(i).getId());
                                iFhInfoUsertaskService.insertFhInfoUsertask(newFhInfoUsertask);
                            }
                        }
                    }
                }
            }
            FhInfoUsertask fhInfoUsertask1 = new FhInfoUsertask();
            fhInfoUsertask1.setUserId(fhInfoUsertask.getUserId());
            List<FhInfoUsertask> fhInfoUsertasks = iFhInfoUsertaskService.selectFhInfoUsertaskList(fhInfoUsertask1);
        }

    }


}
